<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Repository\IdentiteRepository;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;

final class MentionController extends AbstractController
{
    #[Route('/mention', name: 'app_mention')]
    public function index(IdentiteRepository $informations): Response
    {
        $informations = [$informations->find(1)];
        return $this->render('mention/index.html.twig', [
            'information' => $informations[0],
        ]);
    }
    #[Route('/mention/modifier', name: 'app_modifmention')]
    public function modifier(IdentiteRepository $informations): Response
    {
        $informations = [$informations->find(1)];
        return $this->render('mention/modif.html.twig', [
            'information' => $informations[0],
        ]);
    }
    #[Route('/mention/modifier/valider', name: 'app_validermodifmention', methods: ['POST'])]
    public function valider(IdentiteRepository $informations, Request $request, EntityManagerInterface $entityManager): Response
    {
        if (!$this->isGranted('ROLE_ADMIN')) {
        throw $this->createAccessDeniedException();
        }
        $nouveauNom = $request->request->get('nom');
        $nouveauAdresse = $request->request->get('adresse');
        $nouveaupropriete = $request->request->get('propriete');
        $nouveauresponsable = $request->request->get('responsable');
        $nouveauconception = $request->request->get('conception');
        $nouveauanimation = $request->request->get('animation');
        $nouveauhebergement = $request->request->get('hebergement');
        $information = $informations->find(1);
        $information->setNom($nouveauNom);
        $information->setAdresse($nouveauAdresse);
        $information->setProprietaire($nouveaupropriete);
        $information->setResponsable($nouveauresponsable);
        $information->setConceptreal($nouveauconception);
        $information->setAnimation($nouveauanimation);
        $information->setHebergement($nouveauhebergement);
        $entityManager->persist($information);
        $entityManager->flush();
        $this->addFlash('success', 'Mentions légales modifiées avec succès !');
        return $this->redirectToRoute('app_mention');
    }
}
